package com.ccsoft.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Ch03WriteChannel {
    public static void main(String[] args) throws IOException {
        // 准备缓冲区
        String str = "test fileChannel";
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());
        byteBuffer.flip();

        // 创建 channel 用于写文件
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\filechannel.txt");
        FileChannel channel = fileOutputStream.getChannel();
        channel.write(byteBuffer);
        channel.close();
    }
}
